package com.example.app.fragment.catalog;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.app.R;
import com.example.app.adapter.catalog.CurrentSubCategoryAdapter;
import com.example.app.api.Apiservice;
import com.example.app.interfaces.ResponseCallBack;
import com.example.app.viewmodel.ResponseData;
import com.example.app.viewmodel.catalog.Catalog;
import com.example.app.viewmodel.catalog.CurrentCategory;
import com.example.app.viewmodel.catalog.CurrentSubCategory;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CatalogcontentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CatalogcontentFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    ImageView iv_catalog_current_img;
    TextView tv_catalog_current_text;
    GridView gv_catalog_current;

    private static final String ARG_CATEGORY_ID = "category_id";

    public CatalogcontentFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CatalogcontentFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CatalogcontentFragment newInstance(String param1, String param2) {
        CatalogcontentFragment fragment = new CatalogcontentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    // ========== 提供 newInstance 方法接收参数 ==========
    public static CatalogcontentFragment newInstance(String categoryId) {
        CatalogcontentFragment fragment = new CatalogcontentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_CATEGORY_ID, categoryId);
        fragment.setArguments(args);
        return fragment;
    }

    public void initView(View view){
        iv_catalog_current_img = view.findViewById(R.id.iv_catalog_current_img);
        tv_catalog_current_text = view.findViewById(R.id.tv_catalog_current_text);
        gv_catalog_current = view.findViewById(R.id.gv_catalog_current);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_catalogcontent, container, false);

        String categoryId = getArguments() != null ? getArguments().getString(ARG_CATEGORY_ID) : "";
        int id = Integer.parseInt(categoryId);

        Log.e("TAG", "onCreateView: " + id);

        //初始化
        initView(view);

        //获取数据
        Apiservice.GetCatalogCurrent(new ResponseCallBack<ResponseData<Catalog>>() {
            @Override
            public void Success(ResponseData<Catalog> responseData) {
                CurrentCategory currentCategory = responseData.getData().getCurrentCategory();
                List<CurrentSubCategory> currentSubCategoryList = responseData.getData().getCurrentSubCategory();
                //1
                Glide.with(view.getContext())
                        .load(currentCategory.getPicUrl())
                        .into(iv_catalog_current_img);
                //2
                tv_catalog_current_text.setText(currentCategory.getName());
                //3
                gv_catalog_current.setAdapter(new CurrentSubCategoryAdapter(view.getContext(),currentSubCategoryList));
            }
        },id);

        return view;
    }
}